(function(){

YAHOO.pendular.RepositoryKeywordDataSource = function(config){
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        repository: YAHOO.pendular.app.repository
    });
    this.config = config;
    YAHOO.pendular.RepositoryKeywordDataSource.superclass.constructor.call(    
        this, 
        this.searchRepository,
        config
    );
    this.onKeywordsFound = new YAHOO.util.CustomEvent("onKeywordsFound"); 
};

YAHOO.extend(YAHOO.pendular.RepositoryKeywordDataSource, YAHOO.util.FunctionDataSource, {
    searchRepository: function(query) {        
        var config = this.config,
            repository = config.repository,
            repositoryKeywords = repository.keywords,
            keywords = repositoryKeywords.makeKeywords(
                decodeURIComponent(query)
            ),
            numKeywords = keywords.length,
            entries, entry, key, foundKeywords = [];
        ;
        if (!numKeywords) {
            return [];
        }
        //get the last keyword from the query.
        //the last one is special because it uses prefix matching instead of exact
        var lastKeyword = keywords.pop().toUpperCase();
        var keywordDictionary;
        if (keywords.length) {
            //we have more than just the last keyword
            this.prefix = keywords.join(" ") + " ";
            //get all exact matches for the leading keywords from the repository 
            entries = repositoryKeywords.getKeywordEntries(keywords);
            //store the matching entries in a new local dictionary
            keywordDictionary = new YAHOO.pendular.KeywordDictionary();
            for (key in entries) {
                keywordDictionary.addEntry(entries[key]);
            }
            //remove the leading keywords since we don't want to offer those
            //as if they are new suggestions
            keywordDictionary.removeKeywords(keywords);
            keywordDictionary.updateStats();
        }        
        else {
            //no leading keywords, so match against entire repository
            this.prefix = "";
            keywordDictionary = repositoryKeywords;
        }
        this.onKeywordsFound.fire(keywords, keywordDictionary);
        //find keyword suggestions based on prefix match with the last keyword.
        keywordDictionary.eachKeyword(function(key, value){
            if (lastKeyword==="" || (key.indexOf(lastKeyword)===0)) {
                foundKeywords.push(value)
            }
        });
        foundKeywords = foundKeywords.sort(function (a,b){
            a = a.keyword.toUpperCase();
            b = b.keyword.toUpperCase();
            if (a > b) {
                return 1;
            }
            else
            if (a < b) {
                return -1;
            }
            else {
                return 0;
            }
        });
        return foundKeywords;
    }    
});

})();
